package com.liujun.spring4.demo.aop.ioc.base.applicationContextBeans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

/**
 * 1,以car为示例，管理bean生命期的接口
 *
 * @author liujun
 * @version 1.0
 * @date 2-18-08-13 07:02:00
 */
public class Car implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean {

  /** 牌子 */
  private String brand;

  /** 颜色 */
  private String color;

  /** 遇高时速 */
  private int maxSpeed;

  private BeanFactory beanFactory;

  private String beanName;

  public Car() {
    System.out.println("调用Car()构造函数。");
  }

  public void setBrand(String brand) {
    System.out.println("调用setBrand()设置属性");
    this.brand = brand;
  }

  public String getBrand() {
    return brand;
  }

  public String getColor() {
    return color;
  }

  public void setColor(String color) {
    System.out.println("setColor颜色");
    this.color = color;
  }

  public int getMaxSpeed() {
    return maxSpeed;
  }

  public void setMaxSpeed(int maxSpeed) {
    System.out.println("设置速度");
    this.maxSpeed = maxSpeed;
  }

  public void introduce() {
     System.out.println("introduce"+this.toString());
  }


  @Override
  public String toString() {
    final StringBuilder sb = new StringBuilder("Car{");
    sb.append("brand='").append(brand).append('\'');
    sb.append(", color='").append(color).append('\'');
    sb.append(", maxSpeed=").append(maxSpeed);
    sb.append(", beanFactory=").append(beanFactory);
    sb.append(", beanName='").append(beanName).append('\'');
    sb.append('}');
    return sb.toString();
  }

  /**
   * 2,beanFactoryAware接口方法
   *
   * @param beanFactory
   * @throws BeansException
   */
  @Override
  public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    System.out.println("调用BeanFactoryAware.setBeanFactory().");
    this.beanFactory = beanFactory;
  }

  /**
   * 3,beanNameAware接口方法
   *
   * @param name
   */
  @Override
  public void setBeanName(String name) {
    System.out.println("调用beanNameAware.setBeanName");
    this.beanName = beanName;
  }

  /**
   * 4,InitializingBean接口方法
   *
   * @throws Exception
   */
  @Override
  public void afterPropertiesSet() throws Exception {
    System.out.println("调用InitializingBean.afterPropertiesSet().");
  }

  /**
   * 5,DisposableBean接口方法
   *
   * @throws Exception
   */
  @Override
  public void destroy() throws Exception {
    System.out.println("调用DisposableBean.destrory()");
  }

  /** 6通过《bean》的init-method属性指定的初始化方法 */
  public void myInit() {
    System.out.println("调用init-method所指定的myinit(),将Maxspeed设置为240");
    this.maxSpeed = 240;
  }

  /** 7,通过<bean>的destory-method所指定的销毁方法 */
  public void myDestory() {
    System.out.println("调用destory-method所指定的myDestory()");
  }
}
